package com.credit.management.system.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.credit.management.system.custom.dao.CSyuserMapper;
import com.credit.management.system.model.Syuser;



@Controller
@RequestMapping(value="/login")
public class LoginController {
	@Autowired
	private CSyuserMapper cSyuserMapper;
	@RequestMapping(value="/loginVefiry")
	public String loginVefiry(Syuser syuser,Model model,HttpServletRequest request){
		HttpSession session=request.getSession();
		//String password= new SimpleHash("SHA-1",syuser.getName(),syuser.getPassword()).toString();
		UsernamePasswordToken token = new UsernamePasswordToken(syuser.getName(), syuser.getPassword());
		 Subject subject = SecurityUtils.getSubject();
		 
		 try {
			 subject.login(token);
		} catch (AuthenticationException e) {
			
			e.printStackTrace();
			model.addAttribute("msg", "账号密码或错误");
			return "redirect:/login/login";
		}
        if(subject.isAuthenticated()){
        	Syuser syuser1=cSyuserMapper.findUserByName(syuser.getName());
        	//存入session中，其他页面需要
        	session.setAttribute("syuser", syuser1);
        	model.addAttribute("syuser", syuser1);
        	return "redirect:/home/main";
        }else{
        	return "redirect:/login/login";
        }
        
		
		
	}
	@RequestMapping(value="/login")
	public String login(){
		return "common/login";
	}
}
